package de.easywakeup.gui;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;

import android.app.Activity;
import android.database.Cursor;
import android.graphics.Color;
import android.net.Uri;
import android.os.Bundle;
import android.provider.BaseColumns;
import android.provider.MediaStore;
import android.provider.MediaStore.Audio.AudioColumns;
import android.provider.MediaStore.MediaColumns;
import android.view.MotionEvent;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;
import de.easywakeup.R;
import de.easywakeup.data.Data;

public class ChooseMusikAlert extends Activity
{
	private final int WHITE = 0xFFFFFFFF;
	private final int BLACK = 0xFF000000;

	private Data data;

	private ArrayList< HashMap< String, String >> songsList;
	@SuppressWarnings( "unused" )
	private String extraFlag;

	@Override
	protected void onCreate( Bundle savedInstanceState )
	{
		super.onCreate( savedInstanceState );
		setContentView( R.layout.activity_choose_musik );

		extraFlag = getIntent( ).getStringExtra( "Alarm" );

		// Get the global root object
		data = Data.singleton( );

		searchMusikOnSDCard( );

		// find elements from the xml file
		LinearLayout musikList = (LinearLayout) findViewById( R.id.gridLayoutChooseMusik );
		TextView musikTitle;

		// Collecting and creating of the songlist
		for( int i = 0; i < songsList.size( ); i++ )
		{
			musikTitle = new TextView( this );
			musikTitle.setText( songsList.get( i ).get( "songTitle" ) );
			musikTitle.setId( i );
			musikTitle.setTextColor( Color.BLACK );
			musikTitle.setOnTouchListener( new CustomTouchListener( ) );

			musikList.addView( musikTitle );
		}
	}

	public class CustomTouchListener implements View.OnTouchListener
	{

		@Override
		public boolean onTouch( View view, MotionEvent motionEvent )
		{
			switch( motionEvent.getAction( ))
			{
				case MotionEvent.ACTION_DOWN:

					( (TextView) view ).setTextColor( WHITE ); // white
					setCurMusikinData( (TextView) view );

					break;
				case MotionEvent.ACTION_CANCEL:
				case MotionEvent.ACTION_UP:
					( (TextView) view ).setTextColor( BLACK ); // black
					break;
			}
			return false;
		}

	}

	public void searchMusikOnSDCard( )
	{
		songsList = new ArrayList< HashMap< String, String >>( );
		String[ ] STAR = { "*" };

		Cursor cursor;
		Uri uri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
		String selection = AudioColumns.IS_MUSIC + " != 0";

		cursor = getContentResolver( ).query( uri, STAR, selection, null, null );

		if( cursor != null )
		{
			if( cursor.moveToFirst( ) )
			{
				do
				{
					String songName = cursor.getString( cursor.getColumnIndex( MediaColumns.DISPLAY_NAME ) );

					String path = cursor.getString( cursor.getColumnIndex( MediaColumns.DATA ) );
					String title = cursor.getString( cursor.getColumnIndex( MediaColumns.TITLE ) );
					String id = cursor.getString( cursor.getColumnIndex( BaseColumns._ID ) );
					String albumName = cursor.getString( cursor.getColumnIndex( AudioColumns.ALBUM ) );
					int albumId = cursor.getInt( cursor.getColumnIndex( AudioColumns.ALBUM_ID ) );

					HashMap< String, String > song = new HashMap< String, String >( );
					song.put( "songTitle", songName + "" );
					song.put( "realTitle", title + "" );
					song.put( "albumId", albumId + "" );
					song.put( "albumName", albumName + "" );
					song.put( "songPath", path );
					song.put( "id", id );
					songsList.add( song );

				}
				while( cursor.moveToNext( ) );

			}

		}
	}

	// saves MusikChoose in Data
	private void setCurMusikinData( TextView view )
	{
		Iterator< HashMap< String, String >> i = songsList.iterator( );
		while( i.hasNext( ) )
		{

			HashMap< String, String > element = i.next( );
			String textInView = view.getText( ).toString( );
			if( element.get( "songTitle" ).equals( textInView ) )
			{
				// Save reached Data of Musik
				data.setTemp_chooseMusik( element );
				break;
			}

		}

	}

}